<template>
  <div>
    <el-card class="box-card">
      <div slot="header"
           class="clearfix header-box">
        <span>文章分类</span>
        <el-button type="primary"
                   size="mini"
                   @click="dialogVisible = true">添加分类</el-button>
      </div>
      <!-- 表格区域 -->
      <el-table stype="width: 100%;"
                :data="cateList"
                border
                stripe>
        <el-table-column label="序号"
                         type="index"
                         width="100"></el-table-column>
        <el-table-column label="分类名称"
                         prop="cate_name"></el-table-column>
        <el-table-column label="分类别名"
                         prop="cate_alias"></el-table-column>
        <el-table-column label="操作">
          <template #default = "obj">
            <el-button type="primary"
                       size="mini"
                       @click="showEditDialog(obj.row)">修改</el-button>
            <el-button type="danger"
                       size="mini" @click="remove(obj.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 添加分类 -->
    <el-dialog title="添加文章分类"
               :visible.sync="dialogVisible"
               width="35%"
               @closed="onAddClosed">
      <el-form :model="addForm"
               :rules="addFormRules"
               ref="addFormRef"
               label-width="75px">
        <el-form-item label="分类名称"
                      prop="cate_name">
          <el-input v-model="addForm.cate_name"></el-input>
        </el-form-item>
        <el-form-item label="分类别名"
                      prop="cate_alias">
          <el-input v-model="addForm.cate_alias"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="addCate">添 加</el-button>
      </span>
    </el-dialog>
    <!-- 修改分类 -->
    <el-dialog title="修改文章分类"
               :visible.sync="editVisible"
               width="35%">
    <el-form :model="editForm"
             :rules="addFormRules"
             ref="editFormRef"
             label-width="75px">
      <el-form-item label="分类名称"
                    prop="cate_name">
        <el-input v-model="editForm.cate_name"></el-input>
      </el-form-item>
      <el-form-item label="分类别名"
                    prop="cate_alias">
        <el-input v-model="editForm.cate_alias"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer"
            class="dialog-footer">
        <el-button @click="editVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="editCate">添 加</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: 'ArtCate',
  data () {
    return {
      cateList: [],
      dialogVisible: false,
      addForm: {
        cate_name: '',
        cate_alias: ''
      },
      addFormRules: {
        cate_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { pattern: /^\S{1,10}$/, message: '分类名称为非空字符串、长度为 1-10', trigger: 'blur' }
        ],
        cate_alias: [
          { required: true, message: '请输入分类别名', trigger: 'blur' },
          { pattern: /^[a-zA-Z0-9]{1,15}$/, message: '分类别名只能是字母数字、长度为 1-15', trigger: 'blur' }
        ]
      },
      editVisible: false,
      editForm: {
        cate_name: '',
        cate_alias: ''
      },
      isShowEdit: false
    }
  },
  created () {
    this.initCateList()
  },
  methods: {
    async initCateList () {
      const { data: res } = await this.$http.get('/my/cate/list')
      if (res.code !== 0) return this.$message.error('获取文章分类失败！')
      this.cateList = res.data
    },
    onAddClosed () {
      this.$refs.addFormRef.resetFields()
    },
    addCate () {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('/my/cate/add', this.addForm)
        if (res.code !== 0) return this.$message.error('添加分类失败！')
        this.$message.success('添加分类成功！')
        this.initCateList()
        this.dialogVisible = false
      })
    },
    showEditDialog (row) {
      if (row.id === 1 || row.id === 2) {
        if (this.isShowEdit) return
        this.isShowEdit = true
        this.$message({
          message: '管理员不允许修改这个分类！',
          type: 'error',
          onClose: () => {
            this.isShowEdit = false
          }
        })
        return
      }
      this.editForm = Object.assign({}, row)
      this.editVisible = true
    },
    editCate () {
      this.$refs.editFormRef.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.put('/my/cate/info', this.editForm)
        if (res.code !== 0) return this.$message.error('修改分类失败！')
        this.$message.success('修改分类成功！')
        this.editVisible = false
        this.initCateList()
      })
    },
    async remove (id) {
      if (id === 1 || id === 2) return this.$message.error('管理员不允许删除这个分类！')
      const result = await this.$confirm('此操作将永久删除该分类, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (result === 'cancel') return
      const { data: res } = await this.$http.delete('/my/cate/del', {
        params: { id }
      })
      if (res.code !== 0) return this.$message.error('删除分类失败！')
      this.$message.success('删除分类成功！')
      this.initCateList()
    }
  }
}
</script>

<style lang="less" scoped>
.header-box {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
